package com.example.rouroudate.ui.home.ui

import android.annotation.SuppressLint
import com.example.rouroudate.ui.gallery.ui.DateSettingData
import java.text.SimpleDateFormat
import java.util.*

class CircleDate(val beginDate: Calendar = Calendar.getInstance(), var settingData: DateSettingData = DateSettingData(26, 5)) {

    var year
        get() = beginDate[Calendar.YEAR]
        set(value) = beginDate.set(Calendar.YEAR, value)

    var month
        get() = beginDate[Calendar.MONTH] + 1
        set(value) = beginDate.set(Calendar.MONTH, value - 1)

    var day
        get() = beginDate[Calendar.DAY_OF_MONTH]
        set(value) = beginDate.set(Calendar.DAY_OF_MONTH, value)

    fun isCurrentDate(): Boolean {
        val todayDate = Calendar.getInstance()
        val endDate = beginDate.clone() as Calendar
        endDate.add(Calendar.DAY_OF_MONTH, settingData.dateSize)
        return todayDate.time <= endDate.time && todayDate.time >= beginDate.time
    }

    fun getDaySize(): Int {
        return beginDate.getActualMaximum(Calendar.DAY_OF_MONTH)
    }

    fun isSameDate(circleDate: CircleDate): Boolean {
        return this.year == circleDate.year && this.month == circleDate.month && this.day == circleDate.day
    }

    fun addDay(len: Int): CircleDate {
        beginDate.add(Calendar.DAY_OF_MONTH, len)
        return this
    }

    @SuppressLint("SimpleDateFormat")
    fun getStr(): String {
        return SimpleDateFormat("yyyy-MM-dd").format(beginDate.time)
    }

    @SuppressLint("SimpleDateFormat")
    fun getMonthDay(): String {
        return SimpleDateFormat("MM.dd").format(beginDate.time)
    }

    fun deepCopy(): CircleDate {
        val newCircleDate = CircleDate()
        newCircleDate.beginDate.time = beginDate.time
        newCircleDate.settingData = settingData.copy()
        return newCircleDate
    }
}